# coding:utf-8
# writingtime: 2022-7-12
# author: wanjun

'''代数和与积算子'''
class SumProduct:

    def T(self, a, b):
        '''
        function: t-norm运算
        :param a:
        :param b:
        :return: t-norm运算结果
        '''
        return a + b - a * b

    def S(self, a, b):
        '''
        function:t-conorm运算
        :param a:
        :param b:
        :return: t-conorm运算结果
        '''
        return a * b

'''Einstein算子'''
class Einstein:
    def T(self, a, b):
        '''
        function: t-norm运算
        :param a:
        :param b:
        :return: t-norm运算结果
        '''
        return (a + b) / (1 + a * b)

    def S(self, a, b):
        '''
        function:t-conorm运算
        :param a:
        :param b:
        :return: t-conorm运算结果
        '''
        return (a * b)/(1+(1-a)*(1-b))


class IVQROFSumProduct:
    def __init__(self, t, s):
        '''
        function：通过t-norm和t-conorm构建区间值广义正交运算规则
        :param t: t-norm模
        :param s: s-conorm模
        '''
        self.s = s
        self.t = t

    def add(self, a, b, q):
        '''
        function: 区间值广义正交加法
        :param a: 区间值广义正交数a
        :param b: 区间值广义正交数b
        :param q: q值
        :return: 加法结果
        '''
        return ([(self.t(a[0][0] ** q, b[0][0] ** q)) ** (1 / q), (self.t(a[0][1] ** q, b[0][1] ** q)) ** (1 / q)],
                [(self.s(a[1][0] ** q, b[1][0] ** q)) ** (1 / q), (self.s(a[1][1] ** q, b[1][1] ** q)) ** (1 / q)])

    def multi(self, a, b, q):
        '''
        function: 区间值广义正交乘法
        :param a: 区间值广义正交数a
        :param b: 区间值广义正交数b
        :param q: q值
        :return: 加法结果
        '''
        return ([(self.s(a[0][0] ** q, b[0][0] ** q)) ** (1 / q), (self.s(a[0][1] ** q, b[0][1] ** q)) ** (1 / q)],
                [(self.t(a[1][0] ** q, b[0][1] ** q)) ** (1 / q), (self.t(a[1][1] ** q, b[1][1] ** q)) ** (1 / q)])


if __name__ == '__main__':
    data_list = [([0.31, 0.24], [0.73, 0.72]), ([0.97, 0.12], [0.12, 0.05]),
                 ([0.8, 0.52], [0.73, 0.15]), ([0.91, 0.49], [0.42, 0.47]), ([0.95, 0.06], [0.19, 0.1])]
    t = Einstein().T
    s = Einstein().S

    operator = IVQROFSumProduct(t, s)
    # ([0.39046004804624673, 0.30236179245544564], [0.4794285466550963, 0.46419471908906995])
    example=operator.add(data_list[0],data_list[0],q=3)
    print(example)
